package api

import (
	"fmt"
	"net/http"
	"wanner_service/database"
	"wanner_service/utils"

	"github.com/gin-gonic/gin"
)

func GetUser(c *gin.Context) {
	_, data := database.GetUser()

	c.JSON(http.StatusOK, gin.H{
		"status": "200",
		"msg":    "获取成功",
		"data":   data,
	})
}

func AddUser(c *gin.Context) {
	var adUser database.User

	c.ShouldBindJSON(&adUser)
	res := database.AddUser(adUser)
	c.JSON(http.StatusOK, utils.ReqNew(res, utils.GetCodeMsg(res), ""))
}

func DoLogin(c *gin.Context) {
	data := map[string]string{
		"token":        "",
		"refreshtoken": "",
	}

	var loginUser database.User
	// 获取参数
	c.ShouldBindJSON(&loginUser)
	//fmt.Printf("%s\n", loginUser)
	// 验证密码
	result, code := loginUser.CheckPassword()
	if result {

		token, _ := utils.GetToken(30)
		refreshtoken, _ := utils.GetToken(60 * 60)
		data["token"] = token
		data["refreshtoken"] = refreshtoken
		fmt.Println(token)
		c.JSON(http.StatusOK, utils.ReqNew(code, utils.GetCodeMsg(code), data))

	} else {

		c.JSON(http.StatusOK, utils.ReqNew(code, utils.GetCodeMsg(code), ""))
	}
	//fmt.Println(result, code)

}
